package com.xg.java.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.xg.java.entity.SysUser;
import com.xg.java.resp.Response;
import com.xg.java.service.SysUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * @description sys_user
 * @author wang
 * @date 2021-11-23
 */
@Slf4j
@RestController
@Api(tags = "用户管理")
@RequestMapping("/loginUser")
public class LoginController {


    @Resource
    private Response response;

    @Resource
    private SysUserService sysUserService;

    /**
     * create by: YHY
     * description: 登录
     * create time: 2020/6/28 17:11
     *
     * @return 登录结果
     */
    @CrossOrigin
    @ResponseBody
    @ApiOperation(value = "POST",notes = "登录结果")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = SysUser.class),
    })
    @RequestMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Response login(@ApiParam(value = "User对象",name="User")SysUser user) {
        log.info("进入登录.....");
        String userName = user.getUserName();
        String password = user.getPassword();
        if (StringUtils.isBlank(userName)) {
            return response.failure(1001, "用户名为空！");
        }
        if (StringUtils.isBlank(password)) {
            return response.failure(1002, "密码为空！");
        }
        SysUser sysUser = this.sysUserService.loginUser(userName,password);
        if(sysUser != null){
            SysUser code = this.sysUserService.loginUserCode(userName,password);
            if (code == null ) {
                // 登录成功返回用户信息
                return this.response.failure(1003,"用户已超限制，请购买更多授权");
            }
        }
        //loginUser.setToken(SecurityUtils.getSubject().getSession().getId().toString());
        //获取当前用户的审核状态
        //UserInfo userInfo = userInfoService.getUserInfoState(loginUser.getUserId());
        //loginUser.setRecognizeState(userInfo.getState());
        //记录用户登录时间
        if (sysUser == null ) {
            // 登录成功返回用户信息
            return this.response.failure(1265,"抱歉用户，用户名或密码错误");
        }else {
            StpUtil.logout(sysUser.getId());
            StpUtil.login(sysUser.getId());
            SaTokenInfo saTokenInfo = StpUtil.getTokenInfo();
            sysUser.setPassword(saTokenInfo.getTokenValue());
            return response.success(200, "登录成功！", sysUser);
        }
    }


    /**
     * 重置密码;
     *
     * @return
     */
    @RequestMapping(value = "/rePassWd", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    @ApiOperation(value = "POST",notes = "重置密码")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK"),
    })
    public Response repasswd(SysUser sysUser) {
        SysUser user = sysUserService.getById(sysUser.getId());
        try {
            if (user == null) {
                log.info("修改密码失败用户不存在.....");
                return response.failure(1001, "修改密码失败用户不存在！");
            } else if (user.getPassword().equals(sysUser.getOldPassword())){
                log.info("开始更新新密码.....");
                user.setPassword(sysUser.getPassword());
                user.setOldPassword(sysUser.getOldPassword());
                boolean r = this.sysUserService.updateById(user);
                if (r) {
                    return response.success(200, "密码重置成功！",user);
                }
            }else return response.failure(1000,"密码验证失败");
        } catch (Exception e) {
            e.printStackTrace();
//            return response.failure(1000,"重置密码失败");
        }
        return response.failure(1000, "重置密码失败");
    }

}
